import os
import setting as c
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout
from PySide6.QtGui import QPixmap

from core.mhdt import download_image

class QALabel(QWidget):
    def __init__(self, parent=None):
        super(QALabel, self).__init__(parent)

        # 创建两个标签
        self.label_q = QLabel()
        self.label_a = [QLabel(), QLabel(), QLabel(), QLabel()]
        
        # 创建垂直布局
        layout = QVBoxLayout()
        # 将标签添加到布局
        layout.addWidget(self.label_q)
        layout_a = QHBoxLayout()
        for a in self.label_a:
            layout_a.addWidget(a)
        layout.addLayout(layout_a)

        # 设置布局到自定义控件
        self.setLayout(layout)
        
        
    def setMaximumHeight(self, h):
        hf = h / 2
        hf = int(hf)
        self.label_q.setMaximumHeight(hf)
        for a in self.label_a:
            a.setMaximumHeight(hf)
            
    def clear(self):
        self.label_q.setText("")
        for a in self.label_a:
            a.setText("")
            a.setPixmap(QPixmap())
            
    def setText(self, qa):
        self.label_q.setText(f"<font color='red'>Q:</font> {qa['Q']}")
        if qa['Atype'] == 1:
            self.label_a[0].setText(f"<font color='green'>A:</font> {qa['A'][0]}")
        elif qa['Atype'] == 2:
            for i, a in enumerate(qa['A']):
                if i > 4:
                    break
                an = a.split(',')
                print(f"an {an}")
                path = f"{c.flag_dir_path}/{an[0]}"
                if not os.path.exists(path):
                    download_image(an[0], path)
                pixmap = QPixmap(path)
                self.label_a[i].setPixmap(pixmap)
        